package com.sk.weichat.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sk.weichat.MyApplication;
import com.sk.weichat.R;
import com.sk.weichat.bean.Friend;
import com.sk.weichat.bean.User;
import com.sk.weichat.bean.message.ChatMessage;
import com.sk.weichat.bean.message.XmppMessage;
import com.sk.weichat.helper.AvatarHelper;
import com.sk.weichat.util.DateUtils;
import com.sk.weichat.util.DisplayUtil;
import com.sk.weichat.util.HtmlUtils;
import com.sk.weichat.util.StringUtils;
import com.sk.weichat.util.TimeUtils;
import com.sk.weichat.view.HeadView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MessageCallAdapter extends BaseQuickAdapter<Friend, BaseViewHolder> {

    private final List<Friend> mNewFriendList;
    private final List<User> mUserList;

    public MessageCallAdapter(List<Friend> newFriendList, ArrayList<User>userList) {
        super(R.layout.item_message_call_list, newFriendList);
        this.mNewFriendList=newFriendList;
        this.mUserList=userList;
    }

    @Override
    protected void convert(BaseViewHolder helper, Friend item) {

        int position=helper.getLayoutPosition();
        helper.addOnClickListener(R.id.layout_call_list_item)
        .addOnClickListener(R.id.img_call_avatar);
        helper.setText(R.id.tv_call_nickname, TextUtils.isEmpty(item.getRemarkName())?item.getNickName():item.getRemarkName());

        HeadView imgAvatar = helper.getView(R.id.img_call_avatar);
        TextView tvContent = helper.getView(R.id.tv_call_content);
        TextView time_tv = helper.getView(R.id.tv_call_time);
        TextView tvNum=helper.getView(R.id.tv_call_num);

        CheckBox checkBoxSex=helper.getView(R.id.checkbox_call_sex);//性别、年龄
        TextView tvAnonymity=helper.getView(R.id.tv_call_anonymity);//匿名或显示距离、登录时间

        //距离、登录时间
        double latitude = MyApplication.getInstance().getBdLocationHelper().getLatitude();
        double longitude = MyApplication.getInstance().getBdLocationHelper().getLongitude();
        String distance = DisplayUtil.getDistanceInfo(latitude, longitude, mUserList.get(position).getLoc());
        if(mUserList.get(position).getSettings()==null){
            tvAnonymity.setVisibility(View.INVISIBLE);
        }else if(mUserList.get(position).getSettings().getShowLastLoginTime()==1&&mUserList.get(position).getSettings().getAllowQueryLoc()==1){//显示登录时间，距离
            if(TextUtils.isEmpty(distance)){
                tvAnonymity.setText(TimeUtils.getFriendlyTimeDesc(helper.itemView.getContext(), mUserList.get(position).getShowLastLoginTime()));
            }else{
                tvAnonymity.setText(distance+" · "+ TimeUtils.getFriendlyTimeDesc(helper.itemView.getContext(), mUserList.get(position).getShowLastLoginTime()));
            }
        }else if(mUserList.get(position).getSettings().getShowLastLoginTime()==1){
            tvAnonymity.setText(TimeUtils.getFriendlyTimeDesc(helper.itemView.getContext(), mUserList.get(position).getShowLastLoginTime()));//只显示登录时间
        }else if(mUserList.get(position).getSettings().getAllowQueryLoc()==1){//只显示距离
            if(TextUtils.isEmpty(distance)){
                tvAnonymity.setVisibility(View.INVISIBLE);
            }else{
                tvAnonymity.setText(distance);
            }
        }else {
            tvAnonymity.setText("隐身");
        }

        //性别
        if(mUserList.get(position).getSex()== 0){//女
            checkBoxSex.setChecked(false);
        }else{
            checkBoxSex.setChecked(true);
        }
        checkBoxSex.setText(DateUtils.getAge(new Date(mUserList.get(position).getBirthday()*1000))+"");

        //头像
        AvatarHelper.getInstance().displayAvatar(item.getOwnerId(), item, imgAvatar);
        //消息发送时间
        time_tv.setText(TimeUtils.getFriendlyTimeDesc(helper.itemView.getContext(), item.getTimeSend()));

        //消息内容
        if (item.getType() == XmppMessage.TYPE_TEXT) {// 文本消息 表情
            String s = StringUtils.replaceSpecialChar(item.getContent());
            CharSequence content = HtmlUtils.transform200SpanString(s.replaceAll("\n", "\r\n"), true);
            // TODO: 这样匹配的话正常消息里的&8824也会被替换掉，
            if (content.toString().contains("&8824")) {// 草稿
                content = content.toString().replaceFirst("&8824", "");
            }
            tvContent.setText(content);
        } else {
            tvContent.setText(HtmlUtils.addSmileysToMessage(
                    ChatMessage.getSimpleContent(helper.itemView.getContext(), item.getType(), item.getContent()),
                    false
            ));
        }

        //未读消息
        if (item.getUnReadNum() < 1) {
            tvNum.setText("");
            tvNum.setVisibility(View.INVISIBLE);
        } else if (item.getUnReadNum() > 99) {
            tvNum.setText("99+");
            tvNum.setVisibility(View.VISIBLE);
        } else {
            tvNum.setText(String.valueOf(item.getUnReadNum()));
            tvNum.setVisibility(View.VISIBLE);
        }


    }
}